package com.dabai.applicationmanager.dialog;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.dabai.applicationmanager.R;
import com.dabai.uitools.utils.ViewUtils;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class TipAlertDialog {

    private static TextView loadTip;
    private static AlertDialog loadDia;

    private static TipAlertDialog INSTANCE = null;


    private TipAlertDialog() {
    }

    /**
     * 获得实例
     *
     * @param context 上下文
     * @return {@link TipAlertDialog}
     */
    public static TipAlertDialog getInstance(Context context) {
        if (INSTANCE == null) {

            INSTANCE = new TipAlertDialog();

            View diaView = ViewUtils.inflateLayout(context, R.layout.dialog_load);

            loadDia = new MaterialAlertDialogBuilder(context, com.dabai.uitools.R.style.DBTheme_Dialog_RoundTheme)
                    .setView(diaView)
                    .setCancelable(false).create();
            loadDia.getWindow().setWindowAnimations(com.dabai.uitools.R.style.DBAnim_Transitions_Via);
            loadDia.getWindow().setGravity(Gravity.TOP);
            loadTip = diaView.findViewById(R.id.textView);

            return INSTANCE;
        }

        return INSTANCE;
    }


    /**
     * 初始化
     *
     * @param context 上下文
     */
    public static void init(Context context) {
        if (loadDia != null) {
            loadDia.dismiss();
        }
        loadDia = null;
        INSTANCE = null;
        loadTip = null;

        INSTANCE = new TipAlertDialog();

        View diaView = ViewUtils.inflateLayout(context, R.layout.dialog_load);
        loadDia = new MaterialAlertDialogBuilder(context, com.dabai.uitools.R.style.DBTheme_Dialog_RoundTheme)
                .setView(diaView)
                .setCancelable(false).create();
        loadDia.getWindow().setWindowAnimations(com.dabai.uitools.R.style.DBAnim_Transitions_Via);
        loadDia.getWindow().setGravity(Gravity.TOP);
        loadTip = diaView.findViewById(R.id.textView);
    }


    /**
     * 显示提示的布局
     *
     * @param text 文本
     */
    public void showTipLayout(@NonNull String text) {

        if (loadDia != null) {
            if (!loadDia.isShowing()) {
                loadDia.show();
            }
            loadTip.setText(text);
        }

    }

    /**
     * 隐藏提示布局
     */
    public void hideTipLayout() {
        if (loadDia != null) {
            loadDia.dismiss();
        }
    }

    /**
     * get提示布局
     */
    public AlertDialog getTipDialog() {
        return loadDia;
    }

    /**
     * 销毁提示布局
     */
    public static void destroyTipLayout() {
        if (loadDia != null) {
            loadDia.dismiss();
        }
        loadDia = null;
        INSTANCE = null;
        loadTip = null;
    }

}